﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ESOL.Common.Entity;
using ESOL.Business.Mst;
using ESOL.Business.Inv;

namespace ESOL.WinUI.Inv
{
    public partial class frmMaintainStorageChangeQuery : BaseForm.frmBaseDialogyQueryForm
    {
        #region Variable
        public string sChangeType { get; set; }
        public InvStorageEntity mSelectInvStorage { get; set; }

        //public InvImportOrderEntity mSelectedImportOrder;
        //private List<InvImportOrderEntity> mSelectedImportOrderList;
        //public List<InvImportOrderEntity> SelectedImportOrderList
        //{
        //    get { return mSelectedImportOrderList; }
        //}
        //public InvDispatchOrderEntity mSelectedDispatchOrder;
        #endregion

        #region Constructor
        public frmMaintainStorageChangeQuery()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void BindComboBox()
        {


            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbChangeType, "INVC", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(repositoryItemGridLookUpEdit1, "INVC", true);

            cmbChangeType.SelectedValue = sChangeType;

            DataTable dt = MstCommonItemBs.GetMstCommonItemListForInvChange();

            DataRow dr = dt.NewRow();
            dt.Rows.InsertAt(dr,0);

            if (cmbChangeType.SelectedValue.ToString() == "INVC02")
            {
                DataTable dt_in = MstCommonItemBs.GetMstCommonItemDtByType("IMT");
                DataRow dr_nul = dt_in.NewRow();
                dt_in.Rows.InsertAt(dr_nul,0);
                cmbCodeType.DataSource = dt_in;
            }
            else if (cmbChangeType.SelectedValue.ToString() == "INVC01")
            {
                DataTable dt_out = MstCommonItemBs.GetMstCommonItemDtByType("IDT");
                DataRow dr_nul = dt_out.NewRow();
                dt_out.Rows.InsertAt(dr_nul,0);
                cmbCodeType.DataSource = dt_out;
            }
            else
            {
                cmbCodeType.DataSource = dt;
            }
            
            //cmbCodeType.DataSource = dt;
            cmbCodeType.ValueMember = "ITEM_CODE";
            cmbCodeType.DisplayMember = "ITEM_NAME";
            repositoryItemGridLookUpEdit2.DataSource = dt;
            repositoryItemGridLookUpEdit2.DisplayMember = "ITEM_NAME";
            repositoryItemGridLookUpEdit2.ValueMember = "ITEM_CODE";

            
        }
        private string GetCondition()
        {
            string condition = "";
            if (dtpChangeDateFrom.EditValue!=null)
            {
                condition += " AND A.CHANGE_TIME >= CONVERT(DATETIME,'" + dtpChangeDateFrom.EditValue + "')";
            }
            if (dtpChangeDateTo.EditValue!=null)
            {
                condition += " AND A.CHANGE_TIME <= CONVERT(DATETIME,'" + dtpChangeDateTo.EditValue + "')";
            }
            return condition;
        }
        private void BindDataGrid()
        {
            string condition = GetCondition();
            string orderBy = "A.CHANGE_TYPE";
            string sCodeType = "";
            string ChangeType = "";
            string sProduct_id = mSelectInvStorage != null ? mSelectInvStorage.ProductId : "";
            //if (string.IsNullOrEmpty(sChangeType) == false)
            //{
            //    if (string.IsNullOrEmpty(cmbChangeType.Text) == false && cmbChangeType.SelectedValue != null)
            //    {
            //        sCodeType = cmbCodeType.SelectedValue.ToString();
            //    }
            //}
            //if (string.IsNullOrEmpty(sChangeType))
            //{
            //    ChangeType = cmbChangeType.SelectedValue != null ? cmbChangeType.SelectedValue.ToString() : "";
            //}
            //else
            //{
            //    ChangeType = sChangeType;
            //}

            ChangeType = cmbChangeType.SelectedValue.ToString();
            //if (cmbCodeType.SelectedValue.ToString() != "IDT06" && cmbCodeType.SelectedValue.ToString() != "IMT06")
            //{
                sCodeType = cmbCodeType.SelectedValue.ToString();
            //}
            //else
            //{
            //    sCodeType = "";
            //}

            DataTable dt = InvStorageChangeBs.GetByCondition(condition, orderBy, sProduct_id, ChangeType, sCodeType);
            DataTable dt_new = new DataTable();
            dt_new.Columns.Add("PRODUCT_CODE");
            dt_new.Columns.Add("PRODUCT_NAME");
            dt_new.Columns.Add("CHANGE_TYPE");
            dt_new.Columns.Add("ORDER_TYPE");
            dt_new.Columns.Add("ORDER_CODE");
            dt_new.Columns.Add("RELATED_ORDER");
            dt_new.Columns.Add("CHANGE_QTY");
            dt_new.Columns.Add("CHANGE_TIME");
            dt_new.Columns.Add("NAME_CN");
            dt_new.Columns.Add("REMARK");
            //dt_new.Rows.Add(dt.Rows.Count);

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dt_new.Rows.Add();
                dt_new.Rows[i]["PRODUCT_CODE"] = dt.Rows[i]["PRODUCT_CODE"].ToString();
                dt_new.Rows[i]["PRODUCT_NAME"] = dt.Rows[i]["PRODUCT_NAME"].ToString();
                dt_new.Rows[i]["CHANGE_TYPE"] = dt.Rows[i]["CHANGE_TYPE"].ToString();
                if (dt.Rows[i]["CHANGE_TYPE"].ToString() == "INVC02")//入库
                {
                    //if (dt.Rows[i]["IM_CODE"].ToString().Trim() != "")
                    //{
                        // cORDER_TYPE.DataSource = null;
                        //UICommon.UICommonFunctions.BindGridComboboxByCommonItem(cORDER_TYPE, "IMT", true);
                        dt_new.Rows[i]["ORDER_TYPE"] = dt.Rows[i]["IM_TYPE"].ToString();
                        dt_new.Rows[i]["ORDER_CODE"] = dt.Rows[i]["IM_CODE"].ToString();
                        dt_new.Rows[i]["RELATED_ORDER"] = dt.Rows[i]["IM_R_ORDER"].ToString();
                        dt_new.Rows[i]["REMARK"] = dt.Rows[i]["IM_REMARK"].ToString();
                    //}
                    //else
                    //{
                    //    dt_new.Rows[i]["ORDER_TYPE"] = "IMT06";
                    //    dt_new.Rows[i]["ORDER_CODE"] = dt.Rows[i]["DV_CODE"].ToString();
                    //    dt_new.Rows[i]["RELATED_ORDER"] = dt.Rows[i]["DV_R_ORDER"].ToString();
                    //}
                }
                else
                {
                    //if (dt.Rows[i]["DT_CODE"].ToString().Trim() != "")
                    //{
                        // cORDER_TYPE.DataSource = null;
                        //UICommon.UICommonFunctions.BindGridComboboxByCommonItem(cORDER_TYPE, "IDT", true);
                        dt_new.Rows[i]["ORDER_TYPE"] = dt.Rows[i]["DT_TYPE"].ToString();
                        dt_new.Rows[i]["ORDER_CODE"] = dt.Rows[i]["DT_CODE"].ToString();
                        dt_new.Rows[i]["RELATED_ORDER"] = dt.Rows[i]["DT_R_ORDER"].ToString();
                        dt_new.Rows[i]["REMARK"] = dt.Rows[i]["DT_REMARK"].ToString();
                    //}
                    //else
                    //{
                    //    dt_new.Rows[i]["ORDER_TYPE"] = "IDT06";
                        //dt_new.Rows[i]["ORDER_CODE"] = dt.Rows[i]["DV_CODE"].ToString();
                        //dt_new.Rows[i]["RELATED_ORDER"] = dt.Rows[i]["DV_R_ORDER"].ToString();
                    //}
                }
                dt_new.Rows[i]["CHANGE_QTY"] = dt.Rows[i]["CHANGE_QTY"].ToString();
                dt_new.Rows[i]["CHANGE_TIME"] = dt.Rows[i]["CHANGE_TIME"].ToString();
                dt_new.Rows[i]["NAME_CN"] = dt.Rows[i]["NAME_CN"].ToString();
            }



            gvMain.DataSource = dt_new;
        }
        #endregion

        private void frmMaintainStorageChangeQuery_Load(object sender, EventArgs e)
        {
            dtpChangeDateFrom.EditValue = DateTime.Now.AddMonths(-1);
            BindComboBox();
            BindDataGrid();

        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        private void cmbChangeType_DropDownClosed(object sender, EventArgs e)
        {
            sChangeType = cmbChangeType.SelectedValue.ToString();
            BindComboBox();
            BindDataGrid();
        }






    }
}